package com.example.redis.bit;

import com.example.redis.bit.enums.DataModeEnum;
import com.example.redis.bit.domain.IActDomain;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.util.HashMap;
import java.util.Map;

public class ActInstanceFactory implements BeanPostProcessor {

    private static DataModeEnum dataMode;

    private static Map<DataModeEnum, IActDomain> map = new HashMap<>();

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(IActDomain.class.isAssignableFrom(bean.getClass())){
            IActDomain domain = (IActDomain)bean;
            map.put(domain.getMode(),domain);
        }
        return bean;
    }

    public static IActDomain getInstance(){
        return map.get(dataMode);
    }

    @Value("${dataMode}")
    public void setDataMode(DataModeEnum dataMode) {
        ActInstanceFactory.dataMode = dataMode;
    }
}
